package cn.tinlan.shopcoin.app.koi;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


import com.google.gson.JsonObject;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.CallBackProxy;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import org.json.JSONObject;

import butterknife.BindView;
import cn.tinlan.shopcoin.R;
import cn.tinlan.shopcoin.Utils.SPUtils;
import cn.tinlan.shopcoin.Utils.ToastUtils;
import cn.tinlan.shopcoin.app.koi.model.KoiSeekModel;
import cn.tinlan.shopcoin.base.BaseActivity;
import cn.tinlan.shopcoin.base.CustomApiResult;
import cn.tinlan.shopcoin.widget.category.Constants;

/**
 * Created by Mortimer on 2018/12/24. 09:44
 * 佛祖🙏，永无bug。
 */
public class KoiSeekActivity extends BaseActivity implements View.OnClickListener{

//    @BindView(R.id.koi_seek_title)
//    TitleBar titleBar;
//    @BindView(R.id.koi_seek_mobile)
//    TextView seekMobile;
//    @BindView(R.id.koi_seek_level)
//    TextView seekLevel;
//    @BindView(R.id.koi_seek_mfc)
//    TextView seekMFC;
//    @BindView(R.id.koi_seek_ofc)
//    EditText seekOFC;
//
//    @BindView(R.id.koi_seek_rcode)
//    TextView seekRCode;
//
//    @BindView(R.id.koi_seek_pp)
//    TextView seekPP;
//    @BindView(R.id.koi_seek_dcc)
//    TextView seekDCC;
//    @BindView(R.id.koi_seek_dccrefresh)
//    TextView seekDCCRresh;
//
//    @BindView(R.id.koi_seek_sp)
//    TextView seekSP;
//    @BindView(R.id.koi_seek_pp1)
//    TextView seekPP1;
//    @BindView(R.id.koi_seek_join)
//    Button seekJoinBtn;
//
//
//    @BindView(R.id.seek_fans_code)
//    EditText seekFcode;
//    @BindView(R.id.seek_send_point)
//    EditText seekSPoint;
//
//    @BindView(R.id.koi_seek_send)
//    TextView seekSend;


    private boolean isRefresh = false;
    private String fields_id = "";
    private String r_code    = "";
    @Override
    protected int provideContentViewId() {
        return R.layout.activity_koi_seek;
    }

    @Override
    public void initView() {
        super.initView();
        initTitleView();
    }

    private void initTitleView() {
//        titleBar.setOnTitleBarListener(new OnTitleBarListener() {
//            @Override
//            public void onLeftClick(View v) {
//                finish();
//            }
//
//            @Override
//            public void onTitleClick(View v) {
//
//            }
//
//            @Override
//            public void onRightClick(View v) {
//
//            }
//        });
    }

    @Override
    public void initData() {
        super.initData();
        bindSubViewsClickEvent();
        loadUserData();

    }

    private void loadUserData() {

        EasyHttp.post(Constants.KoiUserInfo)
                .params("user_id", SPUtils.getUserID(KoiSeekActivity.this))
                .execute(new CallBackProxy<CustomApiResult<KoiSeekModel>, KoiSeekModel>(new SimpleCallBack<KoiSeekModel>() {
                    @Override
                    public void onError(ApiException e) {
//                        ToastUtils.show("添加失败");

                    }

                    @Override
                    public void onSuccess(KoiSeekModel koiSeekModel) {


//                        ToastUtils.show("添加成功");

                    }
                }) {
                });
    }



    private void bindSubViewsClickEvent() {
//        seekRCode.setOnClickListener(this);
//        seekSend.setOnClickListener(this);
//        seekDCCRresh.setOnClickListener(this);
//        seekJoinBtn.setOnClickListener(this);
//
//        seekSend.setSelected(false);
//        seekJoinBtn.setSelected(false);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.koi_seek_rcode:
                getRandCode();
                break;
            case R.id.koi_seek_dccrefresh:
                toDCCRfresh();
                break;
            case R.id.koi_seek_join:
                joinNewPerson();
                break;
            case R.id.koi_seek_send:
                sendPoint();
                break;
        }
    }


    private void getRandCode() {

        EasyHttp.get(Constants.KoiGetRandCode)
                .params("user_id", SPUtils.getUserID(KoiSeekActivity.this))
                .execute(new CallBackProxy<CustomApiResult<String>, String>(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
//                        ToastUtils.show("添加失败");

                    }

                    @Override
                    public void onSuccess(String s) {

                        try {
                            JSONObject data = new JSONObject(s);
                            JSONObject data1 = data.getJSONObject("data");
                            fields_id = data1.getString("fields_id");
                             r_code = data1.getString("r_code");
//                            seekOFC.setText(r_code);
                        }catch (Exception e){
                            e.printStackTrace();
                        }
//                        ToastUtils.show("添加成功");
                    }
                }) {
                });
    }


    private void toDCCRfresh() {
        EasyHttp.post(Constants.KoiRefreshDCC)
                .params("mobile", "")
                .execute(new CallBackProxy<CustomApiResult<String>, String>(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
//                        ToastUtils.show("添加失败");

                    }

                    @Override
                    public void onSuccess(String s) {
                        JsonObject object = new JsonObject();

                        try {
                            JSONObject data = new JSONObject(s);
                            String num = data.getString("data");
//                            seekDCC.setText(num);
                        }catch (Exception e){
                            e.printStackTrace();
                        }



//                        ToastUtils.show("添加成功");
                    }
                }) {
                });
    }

    private void joinNewPerson() {

//       if (seekJoinBtn.isSelected()) return;

        if (fields_id.isEmpty() || r_code.isEmpty()){
            ToastUtils.show("请先获取推荐码");
            return;
        }


//        seekJoinBtn.setSelected(!seekJoinBtn.isSelected());

        EasyHttp.post(Constants.KoiNewPersonJ)
                .params("fields_id",fields_id)
                .params("r_code",r_code)
                .params("user_id", SPUtils.getUserID(KoiSeekActivity.this))
                .execute(new CallBackProxy<CustomApiResult<String>, String>(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
//                        ToastUtils.show("添加失败");
//                        seekJoinBtn.setSelected(false);
                    }

                    @Override
                    public void onSuccess(String s) {

//                        seekJoinBtn.setSelected(false);
//                        ToastUtils.show("添加成功");
                    }
                }) {
                });
    }


    private void sendPoint() {

//        if (seekSend.isSelected()) return;

//        String r_code = seekFcode.getText().toString().trim();
        if (r_code.isEmpty()){
            ToastUtils.show("请填写您的Fans码");
            return;
        }
//        String point = seekSPoint.getText().toString().trim();
//        if (point.isEmpty()) {
//            ToastUtils.show("请输入积分");
//            return;
//        }

//        seekSend.setSelected(!seekSend.isSelected());

//        EasyHttp.post(Constants.KoiSendPoint)
//                .params("r_code",r_code)
//                .params("account",point)
//                .params("user_id", SPUtils.getUserID(KoiSeekActivity.this))
//                .execute(new CallBackProxy<CustomApiResult<String>, String>(new SimpleCallBack<String>() {
//                    @Override
//                    public void onError(ApiException e) {
////                        ToastUtils.show("添加失败");
//                        seekSend.setSelected(false);
//                    }
//
//                    @Override
//                    public void onSuccess(String s) {
//
//                        seekSend.setSelected(false);
//                        ToastUtils.show("转赠成功");
//                    }
//                }) {
//                });
    }

}
